package com.shop.admin.controller.paper;

import com.shop.admin.aop.Log;
import com.shop.admin.service.IPaperService;
import com.shop.admin.validate.commons.IdValidate;
import com.shop.admin.validate.commons.PageValidate;
import com.shop.admin.validate.paper.PaperCreateValidate;
import com.shop.admin.validate.paper.PaperSearchValidate;
import com.shop.admin.validate.paper.PaperSetStatusValidate;
import com.shop.admin.validate.paper.PaperUpdateValidate;
import com.shop.admin.vo.paper.PaperDetailVo;
import com.shop.admin.vo.paper.PaperListedVo;
import com.shop.common.core.AjaxResult;
import com.shop.common.core.PageResult;
import com.shop.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("${app.api-prefix}/paper")
@Api(tags = "考卷库管理")
public class PaperController {

    @Resource
    IPaperService iPaperService;

    @GetMapping("/list")
    @ApiOperation(value="考卷库列表")
    public AjaxResult<PageResult<PaperListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated PaperSearchValidate searchValidate) {
        PageResult<PaperListedVo> list = iPaperService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="考卷库详情")
    public AjaxResult<PaperDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        PaperDetailVo detail = iPaperService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "考卷库新增")
    @PostMapping("/add")
    @ApiOperation(value="考卷库新增")
    public AjaxResult<Object> add(@RequestBody @Valid PaperCreateValidate createValidate) {
        iPaperService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "考卷库编辑")
    @PostMapping("/edit")
    @ApiOperation(value="考卷库编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody PaperUpdateValidate updateValidate) {
        iPaperService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "设置考卷状态")
    @PostMapping("/setStatus")
    @ApiOperation(value="设置考卷状态")
    public AjaxResult<Object> setStatus(@Validated @RequestBody PaperSetStatusValidate setStatusValidate) {
        iPaperService.setStatus(setStatusValidate.getId(), setStatusValidate.getStatus());
        return AjaxResult.success();
    }

    @Log(title = "考卷库删除")
    @PostMapping("/del")
    @ApiOperation(value="考卷库删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iPaperService.del(idValidate.getId());
        return AjaxResult.success();
    }



}
